<?php

class Recursos_Model_RecursosMapper
{
	protected $_dbTable;
		
	public function setDbTable($dbTable) {
		if (is_string ( $dbTable )) {
			$dbTable = new $dbTable ();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception ( 'Invalid table data gateway provided' );
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable ( 'Recursos_Model_DbTable_Recursos' );
		}
		return $this->_dbTable;
	}
	
	private  function _fillRecurso($row){
		$recurso = new Recursos_Model_Recursos();
		$recurso->setId($row->id)->setRecurso($row->recurso);
		return $recurso;
	}
	
	public function fetchAll()
	{
		$sql = $this->getDbTable()->select()->order(array("recurso ASC"));
		$resultSet = $this->getDbTable()->fetchAll($sql);
		$entries   = array();
		foreach ($resultSet as $row) {
			$entries[] = $this->_fillRecurso($row);
		}
		return $entries;
	}
	
	public function editRecursos(Recursos_Model_Recursos $registro, $id = null) {
		$data = array (
				'recurso' => $registro->getRecurso (),
		);
		$this->getDbTable ()->update ( $data, array (
				'id = ?' => $id
		) );
	}
	public function saveRecursos(Recursos_Model_Recursos $registro) {
		$data = array (
				'recurso' => $registro->getRecurso ()
		);
		$this->getDbTable ()->insert ( $data );
	}
	
	public function deletar($id = NULL) {
		try {
			$result = $this->getDbTable ()->find ( $id );
			$this->getDbTable ()->delete ( array (
					'id = ?' => $id
			) );
		} catch ( Zend_Exception $e ) {
			echo $e;
		}
	}
	
	public function find($id, Recursos_Model_Recursos $registro)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$registro = $this->_fillRecurso($row);
	}
	
	public function findpopule($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		return $data = array(
				"id" => $row->id,
				"recurso" => $row->recurso,
		);
	}

}

